package com.hnl.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hnl.core.connection.ConnectionManager;
import com.hnl.core.connection.ConnectionManagerFactory;
import com.hnl.to.MiscType;
import com.hnl.util.FormatUtil;

public class DBMiscTypeDAO implements MiscTypeDAO {
	public static final Logger LOG = LoggerFactory
			.getLogger(DBMiscTypeDAO.class);

	private PreparedStatement saveStmt;
	private PreparedStatement loadStmt;

	protected DBMiscTypeDAO() throws SQLException {
		// Empty protected constructor to avoid unnecessary initialization
	}

	public MiscType saveMiscType(MiscType misctype) {
		ConnectionManager dbConnMgr = ConnectionManagerFactory
				.getConnectionManager(ConnectionManagerFactory.HNL);
		Connection conn = dbConnMgr.getConnection();
		try {
			saveStmt = conn
					.prepareStatement(
							"INSERT INTO misc_type (type_name, type_desc, create_date, create_user, modify_date, modify_user) "
									+ "VALUES (?,?,?,?,?,?)",
							PreparedStatement.RETURN_GENERATED_KEYS);
			saveStmt.setString(1, misctype.getTypeName());
			saveStmt.setString(2, misctype.getTypeDesc());
			saveStmt.setString(3,
					FormatUtil.getXmlDate(misctype.getCreateDate()));
			saveStmt.setInt(4, misctype.getCreateUser());
			saveStmt.setString(5,
					FormatUtil.getXmlDate(misctype.getModifyDate()));
			saveStmt.setInt(6, misctype.getModifyUser());
			saveStmt.executeUpdate();

			ResultSet rs = saveStmt.getGeneratedKeys();
			if (rs.next()) {
				misctype.setMiscTypeID(rs.getInt(1));
			}
			rs.close();
			saveStmt.close();
		} catch (SQLException e) {
			LOG.error("Error saving MiscType.", e);
		} finally {
			dbConnMgr.releaseConnection(conn);
		}
		return misctype;
	}

	public MiscType loadMiscType(Integer misctypeID) {
		ConnectionManager dbConnMgr = ConnectionManagerFactory
				.getConnectionManager(ConnectionManagerFactory.HNL);
		Connection conn = dbConnMgr.getConnection();
		MiscType misctype = null;

		try {
			loadStmt = conn
					.prepareStatement("SELECT misc_type_ID, type_name, type_desc, create_date, create_user, modify_date, modify_user FROM misc_type "
							+ "WHERE misc_type_ID = ?");
			loadStmt.setInt(1, misctypeID);
			ResultSet rs = loadStmt.executeQuery();
			if (rs.next()) {
				misctype = new MiscType();
				misctype.setMiscTypeID(rs.getInt("misc_type_ID"));
				misctype.setTypeName(rs.getString("type_name"));
				misctype.setTypeDesc(rs.getString("type_desc"));
				misctype.setCreateDate(rs.getDate("create_date"));
				misctype.setCreateUser(rs.getInt("create_user"));
				misctype.setModifyDate(rs.getDate("modify_date"));
				misctype.setModifyUser(rs.getInt("modify_user"));
			}
			rs.close();
			loadStmt.close();
		} catch (SQLException e) {
			LOG.error("Error saving MiscType.", e);
		} finally {
			dbConnMgr.releaseConnection(conn);
		}
		return misctype;
	}

	// public Collection<MiscType> loadAllMiscType(){
	// ConnectionManager dbConnMgr =
	// ConnectionManagerFactory.getConnectionManager(ConnectionManagerFactory.HNL);
	// Connection conn = dbConnMgr.getConnection();
	//
	// Collection<MiscType> misctypeCollection = new Vector<MiscType>();
	//
	// try {
	// } catch (SQLException e) {
	// LOG.error("Error saving MiscType.", e);
	// } finally {
	// dbConnMgr.releaseConnection(conn);
	// }
	//
	// }

	// public void updateMiscType(MiscType misctype){
	// ConnectionManager dbConnMgr =
	// ConnectionManagerFactory.getConnectionManager(ConnectionManagerFactory.HNL);
	// Connection conn = dbConnMgr.getConnection();
	// try {
	//
	// } catch (SQLException e) {
	// LOG.error("Error saving MiscType.", e);
	// } finally {
	// dbConnMgr.releaseConnection(conn);
	// }
	//
	// }
}
